package com.zwface.demo.greendao;

import com.blankj.utilcode.util.TimeUtils;
import com.zwface.demo.App;
import com.zwface.demo.model.db.Leave;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
 * created by      张中文
 * on:             2020/4/28 8:49
 * 描述：请假数据库操作
 */
public class LeaveManager
{
    static LeaveDao leaveDao = App.getInstance().getDaoSession().getLeaveDao();

    public static Long addLeave(Leave leave){
        return leaveDao.insert(leave);
    }

    public static List<Leave> queryAllLeave(){
        return leaveDao.queryBuilder().list();
    }

    /**
     * 根据学生id查询请假记录
     * @param stuId
     * @return
     */
    public static List<Leave> queryLeaveByStuId(String stuId){
        return leaveDao.queryBuilder().where(LeaveDao.Properties.StuId.eq(stuId)).list();
    }

    public static void updateLeave(Leave leave){
        leaveDao.update(leave);
    }

    /**
     * 查询今天请假的记录
     * @return
     */
    public static Map<Long,Leave> queryLeavesToday(){
        long current = System.currentTimeMillis();
        long zomeTime = current/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();
        long endTime = zomeTime+24*60*60*1000;
        String todayS = TimeUtils.millis2String(zomeTime);
        String todayE = TimeUtils.millis2String(zomeTime+24*60*60*1000);
        List<Leave> list = leaveDao.queryBuilder().where(LeaveDao.Properties.TimeStart.ge(zomeTime),
                LeaveDao.Properties.TimeStart.le(endTime)).list();
        Map<Long,Leave> map = new HashMap<>();
        //去重，去除学生号相同的
        for (Leave attendance:list){
            map.put(attendance.getStuId(),attendance);
        }
        return map;
    }
}
